﻿using AntdUI;
using CKS.MES.Server.Demo;
using CKS.PLC.Core.Event;
using CKS.PLC.Core.Siemens;
using Microsoft.Extensions.Configuration;
using MySqlX.XDevAPI;
using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CKS.MES.Server
{
    public partial class PlcTable2 : UserControl
    {
        public PlcTable2()
        {
            InitializeComponent();
            this.Load += PlcPage_Load;
            InitTable();
        }

        private void PlcPage_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;
            EventBus.OnPlcHeart += EventBus_OnPlcHeart;
            DataSource.Clear();
        }

        

        BindingList<PlcInfoUIItem> DataSource = new BindingList<PlcInfoUIItem>();

        void InitTable()
        {
            table1.EditMode = TEditMode.None;
            table1.Columns = new ColumnCollection();
            table1.Columns.Add(new Column("Code", "工位") {  }  );
            table1.Columns.Add(new Column("ConnectionState", "连接状态"));
            table1.Columns.Add(new Column("HeartState", "心跳") { });

            table1.VisibleHeader = false;
            DataSource.Add(new PlcInfoUIItem("OP10")
            {
                Id =1,
          
                ConnectionState = new CellTag[] {
                new CellTag("已连接") {  Type = TTypeMini.Primary}  }
                ,HeartState = new CellBadge(TState.Processing) { Fill = Color.Green , DotRatio = 0.6f  }
            });
            DataSource.Add(new PlcInfoUIItem("OP20")
            {
                Id = 2,
            
                ConnectionState = new CellTag[] {
                new CellTag("已连接") {  Type = TTypeMini.Primary}  }
                ,
                HeartState = new CellBadge(TState.Processing) { Fill = Color.Green }
            });
          


            


            table1.Binding(DataSource);

        }

        internal void UpdateState(List<int> onlineIds)
        {


            foreach (var item in DataSource) {
                if(onlineIds.FindIndex(a => a == item.Id) <0)
                {
                    item.IsOnline = true;
                  
                }
                else
                {
                    item.IsOnline = true;
                    
                }
                
            
            }

            UpdateContentLanguage();
        }



        IConfigurationRoot configLanguage = null;

        internal void UpdateLanguage(IConfigurationRoot configBuilder)
        {
            configLanguage = configBuilder;
            foreach (var item in table1.Columns)
            {
                switch (item.Key)
                {
                    case "Code":
                        item.Title = configBuilder["plcTable:workstationCodeHeader"];
                        break;
                    case "Name":
                        item.Title = configBuilder["plcTable:workstationNameHeader"];
                        break;
                    case "State":
                        item.Title = configBuilder["plcTable:statusHeader"];
                        break;
                    case "OkCount":
                        item.Title = configBuilder["plcTable:OkCountHeader"];
                        break;
                    case "NgCount":
                        item.Title = configBuilder["plcTable:NgCountHeader"];
                        break;
                    default:
                        break;
                }
            }

            UpdateContentLanguage();


        }

        /// <summary>
        /// 更新内容语言
        /// </summary>
        public void UpdateContentLanguage()
        {
            if (configLanguage == null)
                return;
            foreach (var row in DataSource)
            {
                string status = "";
                TTypeMini TType = TTypeMini.Primary;
                if (row.IsOnline)
                {
                    status = configLanguage["plcTable:onlineStatus"];
                }
                else
                {
                    TType = TTypeMini.Error;
                    status = configLanguage["plcTable:offlineStatus"];
                }
                row.ConnectionState = new CellTag[] {
                    new CellTag(status) {  Type = TType }  };
            }
        }

        internal void BindPlc(List<ISiemensClient> plcs)
        {
           
           var items = DataSource.ToList();
            foreach (var plc in plcs)
            {
                var item = items.Find(a => a.Id == plc.PlcInfo.Id);
                if (item == null)
                {
                    item = new PlcInfoUIItem(plc.PlcInfo.WorkstationCode);
                    DataSource.Add(item);
                }
                
                item.Id  = plc.PlcInfo.Id;
                item.ConnectionState = plc.IsConnected ? new CellTag[] {
                new CellTag("已连接") {  Type = TTypeMini.Primary  }  } :
                 new CellTag[] {
                new CellTag("未连接") {  Type = TTypeMini.Warn}  };

               
                
                
            }
            
        }

        private void EventBus_OnPlcHeart(ISiemensClient client, int HeartValue)
        {
            try
            {
               var item  = DataSource.ToList().Find(a => a.Id == client.PlcInfo.Id);
                if (item == null)
                    return;
                item.HeartState = HeartValue == 1 ? new CellBadge(TState.Primary) { Fill = Color.Green, DotRatio = 1.05f }
                 : new CellBadge(TState.Error) { Fill = Color.Red, DotRatio = 0.99f };
            }
            catch (Exception)
            {

            }
        }
    }


   
}
